package com.community.web.controller.plot;

import java.util.Arrays;
import java.util.List;

import com.community.body.domain.ZyOwner;
import com.community.body.dto.OwnerDTO;
import com.community.body.service.IZyOwnerService;
import com.community.common.utils.poi.ExcelUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.community.common.annotation.Log;
import com.community.common.core.controller.BaseController;
import com.community.common.core.domain.AjaxResult;
import com.community.common.enums.BusinessType;
import com.community.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;

/**
 * 业主 Controller
 * 
 * @author community
 * @date 2024-04-11
 */
@RestController
@RequestMapping("/plot/owner")
public class ZyOwnerController extends BaseController
{
    @Autowired
    private IZyOwnerService zyOwnerService;

    /**
     * 查询业主 列表
     */
    @PreAuthorize("@ss.hasPermi('plot:owner:list')")
    @GetMapping("/list")
    public TableDataInfo list(OwnerDTO ownerDTO)
    {
        startPage();
        List<OwnerDTO> list = zyOwnerService.selectZyOwnerList(ownerDTO);
        return getDataTable(list);
    }

    /**
     * 导出业主 列表
     */
    @PreAuthorize("@ss.hasPermi('plot:owner:export')")
    @Log(title = "业主 ", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZyOwner zyOwner)
    {
        String[] split = zyOwner.getOwnerPortrait().split(",");
        Long[] longArray = Arrays.stream(split).map(Long::valueOf).toArray(Long[]::new);
        List<OwnerDTO> list = zyOwnerService.selectZyOwnerQuery(longArray);
        ExcelUtil<OwnerDTO> util = new ExcelUtil<OwnerDTO>(OwnerDTO.class);
        util.exportExcel(response, list, "业主 数据");
    }

    /**
     * 获取业主 详细信息
     */
    @PreAuthorize("@ss.hasPermi('plot:owner:query')")
    @GetMapping(value = "/{ownerId}")
    public AjaxResult getInfo(@PathVariable("ownerId") Long ownerId)
    {
        return success(zyOwnerService.selectZyOwnerByOwnerId(ownerId));
    }

    /**
     * 新增业主 
     */
    @PreAuthorize("@ss.hasPermi('plot:owner:add')")
    @Log(title = "业主 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZyOwner zyOwner)
    {
        return toAjax(zyOwnerService.insertZyOwner(zyOwner));
    }

    /**
     * 修改业主 
     */
    @PreAuthorize("@ss.hasPermi('plot:owner:edit')")
    @Log(title = "业主 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZyOwner zyOwner)
    {
        return toAjax(zyOwnerService.updateZyOwner(zyOwner));
    }

    /**
     * 删除业主 
     */
    @PreAuthorize("@ss.hasPermi('plot:owner:remove')")
    @Log(title = "业主 ", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ownerIds}")
    public AjaxResult remove(@PathVariable Long[] ownerIds)
    {
        return toAjax(zyOwnerService.deleteZyOwnerByOwnerIds(ownerIds));
    }
}
